package udp_client;

import java.io.*;
import java.net.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import javax.media.Player;

public class UdpClientSocket {
	
    private byte[] buffer = new byte[1024];

    private DatagramSocket ds = null;

    public UdpClientSocket() throws Exception {
        ds = new DatagramSocket();
    }
    
    public final void setSoTimeout(final int timeout) throws Exception {
        ds.setSoTimeout(timeout);
    }

    public final int getSoTimeout() throws Exception {
        return ds.getSoTimeout();
    }

    public final DatagramSocket getSocket() {
        return ds;
    }

    public final DatagramPacket send(final String host, final int port,
            final byte[] bytes) throws IOException {
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);
        ds.send(dp);
        return dp;
    }

    public final String receive(final String lhost, final int lport)
            throws Exception {
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        ds.receive(dp);
        System.out.println("dp_length:"+dp.getLength());
        dp = new DatagramPacket(buffer, buffer.length);
        ds.receive(dp);
        System.out.println("dp_length2:"+dp.getLength());
        String info = new String(dp.getData(), 0, dp.getLength());
        return info;
    }

    public final void close() {
        try {
            ds.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        String serverHost = "";
        int serverPort = 9877;
        DatagramSocket ds = new DatagramSocket(9988);
        
//        myCamera temp_aCamera=new myCamera();
//        Thread myCamera= new Thread(temp_aCamera);
//        myCamera.start();
        
        myAVI temp_aAVI=new myAVI();
        Thread myAVI= new Thread(temp_aAVI);
        myAVI.start();
        
        ReadImage temp_read=new ReadImage();
        Thread myRead= new Thread(temp_read);
        myRead.start();
        
        mySplit temp_split=new mySplit();
        Thread split= new Thread(temp_split);
        split.start();
        
        mySend temp_send=new mySend(ds, serverHost, serverPort);
        Thread send= new Thread(temp_send);
        send.start();
        
//        DatagramSocket ds2= new DatagramSocket();
        
        myReceive temp_reveive=new myReceive(ds);
        Thread receive= new Thread(temp_reveive);
        receive.start();
        
        myGather temp_gather=new myGather();
        Thread gather= new Thread(temp_gather);
        gather.start();
        
        myDisplay temp_display=new myDisplay();
        Thread display= new Thread(temp_display);
        display.start();
        
//        Thread.sleep(1000000);
//        client.send(serverHost, serverPort, "this is server".getBytes());
//        String info = client.receive(serverHost, serverPort);
//        System.out.println("reponse from the server" + info);
    }
}

